package com.ruoyi.web.controller.caipiao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.caipiao.domain.CaipiaoPl3MethodFive;
import com.ruoyi.caipiao.utils.Pl3NumUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.caipiao.domain.CaipiaoPl3MethodSix;
import com.ruoyi.caipiao.service.ICaipiaoPl3MethodSixService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 方法6预测Controller
 *
 * @author ruoyi
 * @date 2025-06-22
 */
@RestController
@RequestMapping("/caipiao/six")
public class CaipiaoPl3MethodSixController extends BaseController {
    @Autowired
    private ICaipiaoPl3MethodSixService caipiaoPl3MethodSixService;


    /**
     * 生成排列5号码
     *
     * @param caipiaoPl3MethodSix
     * @return
     */
    @PreAuthorize("@ss.hasPermi('caipiao:three:list')")
    @GetMapping("/generateCode")
    public AjaxResult generateCode(CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        List<CaipiaoPl3MethodSix> adds = new ArrayList<>();
        for(int i = 0; i < 300000; i++) {
            Set<String> codes = caipiaoPl3MethodSixService.getCodes();
            if(!codes.isEmpty()) {
                CaipiaoPl3MethodSix save = new CaipiaoPl3MethodSix();
                save.setId(IdUtil.getSnowflakeNextId());
                save.setNums(Pl3NumUtil.set2Str(codes));
                save.setNumsSize(codes.size());
                save.setQs(caipiaoPl3MethodSix.getQs());
                adds.add(save);
            }
            if(!adds.isEmpty() && adds.size() % 1000 == 0) {
                caipiaoPl3MethodSixService.saveBatch(adds);
                logger.info("方法6，保存号码。。。");
            }
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('caipiao:three:list')")
    @GetMapping("/checkCode")
    public AjaxResult checkCode(CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        List<CaipiaoPl3MethodSix> datas = caipiaoPl3MethodSixService.checkCode(caipiaoPl3MethodSix);
        if (CollectionUtil.isNotEmpty(datas)) {
            caipiaoPl3MethodSixService.updateBatchById(datas);
        }
        return success();
    }

    /**
     * 查询方法6预测列表
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:list')")
    @GetMapping("/list")
    public TableDataInfo list(CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        startPage();
        List<CaipiaoPl3MethodSix> list = caipiaoPl3MethodSixService.selectCaipiaoPl3MethodSixList(caipiaoPl3MethodSix);
        return getDataTable(list);
    }

    /**
     * 导出方法6预测列表
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:export')")
    @Log(title = "方法6预测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        List<CaipiaoPl3MethodSix> list = caipiaoPl3MethodSixService.selectCaipiaoPl3MethodSixList(caipiaoPl3MethodSix);
        ExcelUtil<CaipiaoPl3MethodSix> util = new ExcelUtil<CaipiaoPl3MethodSix>(CaipiaoPl3MethodSix.class);
        util.exportExcel(response, list, "方法6预测数据");
    }

    /**
     * 获取方法6预测详细信息
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(caipiaoPl3MethodSixService.selectCaipiaoPl3MethodSixById(id));
    }

    /**
     * 新增方法6预测
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:add')")
    @Log(title = "方法6预测", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        return toAjax(caipiaoPl3MethodSixService.insertCaipiaoPl3MethodSix(caipiaoPl3MethodSix));
    }

    /**
     * 修改方法6预测
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:edit')")
    @Log(title = "方法6预测", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaipiaoPl3MethodSix caipiaoPl3MethodSix) {
        return toAjax(caipiaoPl3MethodSixService.updateCaipiaoPl3MethodSix(caipiaoPl3MethodSix));
    }

    /**
     * 删除方法6预测
     */
    @PreAuthorize("@ss.hasPermi('caipiao:six:remove')")
    @Log(title = "方法6预测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(caipiaoPl3MethodSixService.deleteCaipiaoPl3MethodSixByIds(ids));
    }
}
